
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;

public class SpawnFromMonoBehaviour : MonoBehaviour
{
    public GameObject Prefab;

    public int CountX;

    public int CountY;


    // Start is called before the first frame update
    void Start()
    {
        var settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, null);
        var prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(Prefab, settings);
        var entityMgr = World.DefaultGameObjectInjectionWorld.EntityManager;
        for (int x = 0; x < CountX; x++)
        {
            for (int y = 0; y < CountY; y++)
            {
                // Efficiently instantiate a bunch of entities from the already converted entity prefab
                var instance = entityMgr.Instantiate(prefab);

                // Place the instantiated entity in a grid with some noise
                var position =
                    transform.TransformPoint(new float3(x * 1.3F, noise.cnoise(new float2(x, y) * 0.21F) * 2,
                        y * 1.3F));
                entityMgr.SetComponentData(instance, new Translation { Value = position });
            }

        }

    }
}
